package com.hsd.app.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.hsd.app.model.HsdSynNames;
import com.hsd.app.service.IDataManageService;
import com.hsd.core.base.action.BaseAction;
import com.hsd.core.dbutils.DBMgr;

public abstract class PLBaseAction extends BaseAction{
	 
   /**
    * 存储各类名词
    * @param request
    * @param syname
    * @param dataService
    * @param namestr
    * @param syntype
    * @param wordtype
    * @return
    */
   protected Long checkAndSaveSynames(HttpServletRequest request,String syname,IDataManageService dataService,String namestr,int syntype,int wordtype){
	   DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
	   List synlist = dbo.queryList("select * from hsd_syn_names where type=? and namestr=? and youxiaobj=1 and wordtype=?", new Object[]{syntype,syname,wordtype});
	   if(synlist.size() > 0){
		   //该词已经存在
		   return 0l;
	   }
	   Long seqId = getSynSeqValue(dbo);
	   Long groupId = 0l;
	   //当syntype =1时  标明该词为正名，setGroupid 存储本正名的主键
	   HsdSynNames synObj = new HsdSynNames();
	   synObj.setId(seqId);
	   synObj.setYouxiaobj(1l);
	   synObj.setNamestr(namestr);
	   synObj.setWordtype(new Long(wordtype));
	   synObj.setType(new Long(syntype));
	   if(syntype == 2){
	 	   Map dataMap = dbo.getRowMap("select groupid from hsd_syn_names where namestr=? and wordtype=? and youxiaobj=1 and type=2",new Object[]{syname,wordtype});
           if(dataMap != null){
        	   groupId = (Long)dataMap.get("groupid");
           }
	   }
	   synObj.setGroupid(groupId);
	   synObj.setYouxiaobj(PLCONSTANTS.YOUXIAOBJ_YX); 
	   dataService.saveObject(synObj);
	   return seqId;
   }
   
   /**
    * 获取名词的主键
    * @param request
    * @return
    */
   private Long getSynSeqValue(DBMgr dbo){
 	   Map dataMap = dbo.getRowMap("select nextval('seq_hsd_syn_names_id') as id from dual",null);
	   return new Long((Integer)dataMap.get("id"));
   }
}
